package com.seleniti.pozit.controller.common.domain;

import javax.jdo.annotations.Column;
import javax.jdo.annotations.Extension;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.Inheritance;
import javax.jdo.annotations.InheritanceStrategy;
import javax.jdo.annotations.NotPersistent;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Version;
import javax.jdo.annotations.VersionStrategy;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import com.grupit.domain.jdo.User;
import com.seleniti.pozit.controller.model.integration.database.domain.PoZitNote;

@PersistenceCapable(identityType = IdentityType.APPLICATION)
@Inheritance(strategy = InheritanceStrategy.NEW_TABLE)
@Version(column = "version", strategy = VersionStrategy.VERSION_NUMBER, extensions = { @Extension(vendorName = "datanucleus", key = "field-name", value = "version") })
public class Note extends PoZitNote {
	// private static NoteService poZitService;

	@NotPersistent
	private User ownerUser;

	@NotNull
	@Column(length = 51)
	@Size(max = 51, message = "El 'SystemId' del destinatario no puede sobrepasar los 51 carácteres, de lo contrario es un SystemID inválido")
	private String targetSystemId;

	protected long version;

	public Long getId() {
		return getNoteId();
	}

	public String getTargetSystemId() {
		return targetSystemId;
	}

	public void setTargetSystemId(String targetSystemId) {
		this.targetSystemId = targetSystemId;
	}

	// public void save() throws GrupitEngineException, IOException {
	// NoteService poZitService = ensurePoZitService();
	// poZitService.save(this);
	// }
	//
	// public void delete() throws GrupitEngineException, IOException {
	// NoteService poZitService = ensurePoZitService();
	// poZitService.delete(this);
	// }
	//
	// public static Note findNote(Long noteId) throws GrupitEngineException,
	// IOException {
	// NoteService poZitService = ensurePoZitService();
	// Note note = poZitService.findPoZitById(noteId);
	// return note;
	// }
	//
	// public static List<Note> findAllNotesByUser(User user) throws
	// GrupitEngineException, IOException {
	// NoteService poZitService = ensurePoZitService();
	// List<Note> notes = poZitService.findAllNotesByUser(user);
	// return notes;
	// }
	//
	// protected static NoteService ensurePoZitService() throws
	// GrupitEngineException {
	// if (poZitService == null) {
	// GrupitEngineFacade engine =
	// GrupitEngineConnector.getGrupitEngineFacade();
	// poZitService =
	// engine.getRegisteredPluggableFeatureService(NoteService.class);
	// }
	// return poZitService;
	// }

	public Long getVersion() {
		return version;
	}

	public User getOwnerUser() {
		return ownerUser;
	}

	public void setOwnerUser(User ownerUser) {
		this.ownerUser = ownerUser;
		this.setOwnerUserId(ownerUser != null && ownerUser.getUserId() != null ? ownerUser.getUserId().toString()
				: null);
	}
}
